package com.lgc.login.ui

import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.lgc.common.base.BaseActivity
import com.lgc.login.R
import com.lgc.login.databinding.ActivityTextlivedataBinding
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * 测试liveata数据能不能实时刷新
 */
class TextLiveDataActivity :BaseActivity<ActivityTextlivedataBinding>() {
    val textViewModel by lazy { ViewModelProvider(this).get(TextViewModel::class.java) }
    override fun getLayoutRes(): Int= R.layout.activity_textlivedata
    override fun initData() {
        super.initData()

       mBinding.apply {
           //testvm=textViewModel

        textViewModel.testdata2.observe(this@TextLiveDataActivity, Observer { result ->
            tvData.text=result
        })
           btnChange.setOnClickListener {
               textViewModel.changeText2("change")
           }
           lifecycleScope.launch {
               delay(7000)
               textViewModel.changeText3("延迟了7秒钟")
           }
       }

    }
}


